package com.speaktoitcookbook.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.speaktoitcookbook.bean.User;
import com.speaktoitcookbook.bean.UserInfo;
import com.speaktoitcookbook.dao.IUserDao;
import com.speaktoitcookbook.util.DBUtil;
import com.speaktoitcookbook.util.TransformUtil;

/**
 * 用户帐号相关操作类
 * 
 * @author 杨强
 * 
 */
public class UserDaoImpl implements IUserDao {
    private Connection conn;

    public UserDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(User user, UserInfo userInfo) throws SQLException {
        boolean result = false;
        StringBuffer sqlUser = new StringBuffer(
                "insert into `tb_users`(`userID`, `userPWD`) values(")
                .append(user.getID()).append(", '").append(user.getPassword())
                .append("')");
        StringBuffer sqlUserInfo = new StringBuffer(
                "insert into `tb_userinfo`(`userID`, `nickname`, `userName`, `sex`, `age`, `userFace`, `telephone`, `qq`, `email`, `userIntro`, `registerDate`) values(")
                .append(userInfo.getID()).append(", '")
                .append(userInfo.getNickname()).append("', '")
                .append(userInfo.getUserName()).append("', '")
                .append(userInfo.getSex()).append("', ")
                .append(userInfo.getAge()).append(", '")
                .append(userInfo.getUserFace()).append("', '")
                .append(userInfo.getTelephone()).append("', '")
                .append(userInfo.getQQ()).append("', '")
                .append(userInfo.getEmail()).append("', '")
                .append(userInfo.getUserIntro()).append("', '")
                .append(new Timestamp(userInfo.getRegisterDate().getTime()))
                .append("')");

        List<String> sqls = new ArrayList<String>(2);
        sqls.add(sqlUser.toString());
        sqls.add(sqlUserInfo.toString());
        result = DBUtil.execute(conn, sqls);
        return result;
    }

    @Override
    public boolean doUpdate(User user) throws SQLException {
        boolean result = false;
        String sql = "update `tb_users` set `userID` = ?, `userPWD` = ? where `userID` = ?";
        Object[] params = TransformUtil.beanTOArray(user);
        result = DBUtil.execute(conn, sql, params, params[0]);
        return result;
    }

    @Override
    public boolean doDelete(User user) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_users` where `userID` = ? and `userPWD` = ?";
        Object[] params = TransformUtil.beanTOArray(user);
        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public boolean doDelete(String userID) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_users` where `userID` = ?";
        result = DBUtil.execute(conn, sql, userID);
        return result;
    }

    @Override
    public User getUser(User user) throws SQLException {
        User result = null;
        String sql = "select * from `tb_users` where `userID` = ? and `userPWD` = ?";
        Object[] params = TransformUtil.beanTOArray(user);
        List<Object[]> all = DBUtil.query(conn, sql, params);

        if (all.size() > 0) {
            result = new User();
            user = (User) TransformUtil.arrayToBean(all.get(0), User.class);
        }
        return result;
    }

    @Override
    public User getUser(String userID) throws SQLException {
        User result = null;
        String sql = "select * from `tb_users` where `userID` = ?";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (all.size() > 0) {
            result = new User();
            result = (User) TransformUtil.arrayToBean(all.get(0), User.class);
        }
        return result;
    }

    @Override
    public List<User> getUser(Object... params) throws SQLException {
        List<User> result = null;
        StringBuffer sql = new StringBuffer("select * from `tb_users` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (all.size() > 0) {
            result = new ArrayList<User>(all.size());
            for (Object[] objects : all) {
                User user = null;
                user = (User) TransformUtil.arrayToBean(objects, User.class);
                result.add(user);
            }
        }
        return result;
    }
}
